package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCSaleCommissionPayment;
import com.shrct.businessprocess.project.RCSaleCommissionYearPayment;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.impl.RCSaleCommissionPaymentImpl;
import com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl;
import com.shrct.businessprocess.project.proxy.RCSaleCommissionYearPaymentProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALECOMMISSIONYEARPAYMENT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalecommissionyearpayment")
public class RCSaleCommissionYearPaymentImpl extends BaseModelObject implements RCSaleCommissionYearPayment {
  public RCSaleCommissionYearPaymentImpl() {
    super();
  }
  
  private Integer year;
  
  public Integer getYear() {
    return this.year;
  }
  
  public RCSaleCommissionYearPayment setYear(final Integer year) {
    this.year = year;
    return this;			
    
  }
  
  @JoinColumn(name = "saler_id")
  @ManyToOne(targetEntity = RCSalesAssignmentImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesAssignment saler;
  
  public RCSalesAssignment getSaler() {
    return this.saler;
  }
  
  public RCSaleCommissionYearPayment setSaler(final RCSalesAssignment saler) {
    this.saler = saler;
    return this;			
    
  }
  
  private Double comparableIncomeAmount = Double.valueOf(0d);
  
  public Double getComparableIncomeAmount() {
    return this.comparableIncomeAmount;
  }
  
  public RCSaleCommissionYearPayment setComparableIncomeAmount(final Double comparableIncomeAmount) {
    this.comparableIncomeAmount = comparableIncomeAmount;
    return this;			
    
  }
  
  private Double preDiscount = Double.valueOf(0d);
  
  public Double getPreDiscount() {
    return this.preDiscount;
  }
  
  public RCSaleCommissionYearPayment setPreDiscount(final Double preDiscount) {
    this.preDiscount = preDiscount;
    return this;			
    
  }
  
  private Double ownedAmount = Double.valueOf(0d);
  
  public Double getOwnedAmount() {
    return this.ownedAmount;
  }
  
  public RCSaleCommissionYearPayment setOwnedAmount(final Double ownedAmount) {
    this.ownedAmount = ownedAmount;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCSaleCommissionPaymentImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "yearPayment")
  private Collection<RCSaleCommissionPayment> payments = new java.util.ArrayList<com.shrct.businessprocess.project.RCSaleCommissionPayment>();;
  
  public Collection<RCSaleCommissionPayment> getPayments() {
    return this.payments;
  }
  
  public RCSaleCommissionPayment createAndAddtoPayments() {
    getPayments().size();
    com.shrct.businessprocess.project.RCSaleCommissionPayment rCSaleCommissionPayment = new com.shrct.businessprocess.project.impl.RCSaleCommissionPaymentImpl(this);
    getObjectFactory().create(rCSaleCommissionPayment);
    getPayments().add(rCSaleCommissionPayment);
    return rCSaleCommissionPayment;
    
  }
  
  public RCSaleCommissionPayment createAndAddtoPayments(final Procedure1<RCSaleCommissionPayment> updater) {
    getPayments().size();
    com.shrct.businessprocess.project.RCSaleCommissionPayment rCSaleCommissionPayment = new com.shrct.businessprocess.project.impl.RCSaleCommissionPaymentImpl(this);
    getObjectFactory().create(rCSaleCommissionPayment);
    getPayments().add(rCSaleCommissionPayment);
    return rCSaleCommissionPayment;
    
  }
  
  public RCSaleCommissionYearPayment removeAllPayments() {
    for(RCSaleCommissionPayment obj : this.payments){
    	getObjectFactory().delete(obj);
    }
    this.payments.clear();
    return this;	
    
  }
  
  public RCSaleCommissionYearPayment removeFromPayments(final RCSaleCommissionPayment rCSaleCommissionPayment) {
    this.payments.remove(rCSaleCommissionPayment);
    getObjectFactory().delete(rCSaleCommissionPayment);
    return this;
    
  }
  
  public RCSaleCommissionPayment initializePayment(final Date date) {
    RCSaleCommissionPayment _createAndAddtoPayments = this.createAndAddtoPayments();
    final Procedure1<RCSaleCommissionPayment> _function = new Procedure1<RCSaleCommissionPayment>() {
        public void apply(final RCSaleCommissionPayment it) {
          it.setDate(date);
        }
      };
    RCSaleCommissionPayment _doubleArrow = ObjectExtensions.<RCSaleCommissionPayment>operator_doubleArrow(_createAndAddtoPayments, _function);
    final Procedure1<RCSaleCommissionPayment> _function_1 = new Procedure1<RCSaleCommissionPayment>() {
        public void apply(final RCSaleCommissionPayment it) {
          it.initializePayments4SettlementYear();
        }
      };
    RCSaleCommissionPayment _doubleArrow_1 = ObjectExtensions.<RCSaleCommissionPayment>operator_doubleArrow(_doubleArrow, _function_1);
    return _doubleArrow_1;
  }
  
  public void deletePayment(final RCSaleCommissionPayment payment) {
    this.removeFromPayments(payment);
  }
  
  public void update(final Procedure1<? super RCSaleCommissionYearPayment> updater) {
    updater.apply(this);
    this.checkValid();
  }
  
  public void checkValid() {
    boolean _notEquals = (!Objects.equal(this.year, null));
    Validates.Assert(_notEquals, "\u5E74\u5EA6\u4E0D\u80FD\u4E3A\u7A7A");
    boolean _notEquals_1 = (!Objects.equal(this.saler, null));
    Validates.Assert(_notEquals_1, "\u9500\u552E\u4E0D\u80FD\u4E3A\u7A7A");
  }
  
  public static RCSaleCommissionYearPayment create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSaleCommissionYearPayment rCSaleCommissionYearPayment = new com.shrct.businessprocess.project.impl.RCSaleCommissionYearPaymentImpl(
    );
    objectFactory.create(rCSaleCommissionYearPayment);
    return rCSaleCommissionYearPayment;			
    
  }
  
  public static RCSaleCommissionYearPayment createRCSaleCommissionYearPaymentImpl(final IObjectFactory objectFactory, final Procedure1<RCSaleCommissionYearPayment> init) {
    com.shrct.businessprocess.project.RCSaleCommissionYearPayment rCSaleCommissionYearPayment = new com.shrct.businessprocess.project.impl.RCSaleCommissionYearPaymentImpl(
    );
    objectFactory.create(rCSaleCommissionYearPayment);
    init.apply(rCSaleCommissionYearPayment);
    
    return rCSaleCommissionYearPayment;			
    
  }
  
  public RCSaleCommissionYearPaymentProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSaleCommissionYearPaymentProxy proxy = new com.shrct.businessprocess.project.proxy.RCSaleCommissionYearPaymentProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
